package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategySearchService strategySearchService;

    //攻略主页
    @RequestMapping("")
    public String index(Model model) {
        //hotCds    热门top10
        List<StrategyStatisVO> hotCds = strategyStatisVOService.getHotTop10();
        model.addAttribute("hotCds", hotCds);

        //abroadCds   海外top10
        List<StrategyStatisVO> abroadCds = strategyStatisVOService.getAbroadTop10();
        //System.out.println(abroadCds);
        model.addAttribute("abroadCds", abroadCds);

        //unabroadCds  国内top10
        List<StrategyStatisVO> unabroadCds =  strategyStatisVOService.getUnabroadTop10();
        model.addAttribute("unabroadCds", unabroadCds);

        //themeCds  查询主题推荐(10个主题,排序,每个主题的地区名称不可重复)
        List<Map<String, Object>> themeCds = strategySearchService.queryThemeCommed();
        model.addAttribute("themeCds", themeCds);

        //chinas   国内
        List<StatisVO> chinas = strategySearchService.queryChinaCommed();
        model.addAttribute("chinas",chinas);

        //abroads   国外
        List<StatisVO> abroads = strategySearchService.queryAbroadsCommed();
        model.addAttribute("abroads",abroads);


        //themes    主题
        List<StatisVO> themes = strategySearchService.queryThemesCommed();
        model.addAttribute("themes",themes);


        //commends 推荐攻略(5个)
        model.addAttribute("commends",strategyCommendService.getTop5());

        return "strategy/index";
    }

    //利用el分页高查
    @RequestMapping("/searchPage")
    public String searchPage(Model model, StrategySearchQuery qo ) {
        Page page = strategySearchService.query(qo);
        model.addAttribute("page", page);
        return "strategy/searchPageTpl";
    }


    /**
     * 查询单篇文章
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Model model, Long id, @UserParam UserInfo userInfo) {
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
        model.addAttribute("detail", detail);

        //阅读量增加,可自定义增加量
        strategyStatisVOService.increaseViewnum(id, 1);

        //热门排行(增加攻略热度)
        strategyStatisVOService.addHot(id, 1);

        //isFavor 收藏图标是否亮
        boolean isFavor = false;
        if (userInfo != null) {
            isFavor = strategyStatisVOService.hasFavor(id, userInfo.getId());
        }

        model.addAttribute("isFavor", isFavor);

        //获取vo数据
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(id);
        model.addAttribute("vo", vo);
        return "strategy/detail";
    }

    /**
     * 分页查询
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //面包屑查询
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);

        //tags
        model.addAttribute("tags", strategyTagService.list());

        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";
    }


    /**
     * 保存评论
     * @param strategyComment
     * @param user
     * @return
     */
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment strategyComment, @UserParam UserInfo user) {

        strategyComment.setUserId(user.getId());
        strategyComment.setCity(user.getCity());
        strategyComment.setUsername(user.getNickname());
        strategyComment.setLevel(user.getLevel());
        strategyComment.setHeadUrl(user.getHeadImgUrl());
        strategyCommentService.save(strategyComment);

        //热门排行(增加攻略热度)
        strategyStatisVOService.addHot(strategyComment.getDetailId(), 1);

        //评论增加
        strategyStatisVOService.increaseReplay(strategyComment.getDetailId(), 1);
        //获取评论数
        int replynum = strategyStatisVOService.getStrategyStatisVO(strategyComment.getDetailId()).getReplynum();
        return AjaxResult.success().addData(replynum);
    }

    //评论
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {

        Page page = strategyCommentService.query(qo);

        model.addAttribute("page", page);

        return "strategy/commentTpl";
    }

    /**
     * 点赞功能
     * @param toid
     * @param fromid
     * @param userInfo
     * @return
     */
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, long fromid, @UserParam UserInfo userInfo) {

       strategyCommentService.commentThumbUp(toid, fromid);

        return AjaxResult.success();
    }


    /**
     * 收藏功能
     * @param sid
     * @param userInfo
     * @return
     */
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        //判断是否登录
        if (userInfo == null) {
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setSuccess(false);
            ajaxResult.setCode(102);
            ajaxResult.setMsg("登录后可进行收藏");
            return ajaxResult;
        }

        //收藏
        boolean flag = strategyStatisVOService.addFavornum(sid,userInfo.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(sid);

        //通过收藏或顶增加国内外热度
        if (flag) {
            strategyStatisVOService.addAbroadorUnabroad(sid, 1);
        } else {
            strategyStatisVOService.addAbroadorUnabroad(sid, -1);
        }

        return new AjaxResult(flag, "").addData(vo);
    }

    //顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        //判断是否登录
        if (userInfo == null) {
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setSuccess(false);
            ajaxResult.setCode(102);
            ajaxResult.setMsg("登录后可进行顶");
            return ajaxResult;
        }
        //通过收藏或顶增加国内外热度
        strategyStatisVOService.addAbroadorUnabroad(sid, 1);

        //顶
        boolean flag = strategyStatisVOService.addThumbsupnum(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(sid);

        return new AjaxResult(flag, "").addData(vo);
    }

    @RequestMapping("/share")
    @ResponseBody
    public Object share(Long sid,@UserParam UserInfo userInfo){
        //判断是否登录
        if (userInfo == null) {
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setSuccess(false);
            ajaxResult.setCode(102);
            ajaxResult.setMsg("登录后可进行分享");
            return ajaxResult;
        }
        boolean flag = strategyStatisVOService.addSharenum(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(sid);
        return new AjaxResult(flag, "").addData(vo);
    }

}
